<template>
  <div class="card">
    <div class="login_Bj">
      <el-form :model="user" :rules="userRules" ref="user" label-width="100px">
        <el-form-item style="color: aliceblue;" label="手机号" prop="memberPhone">
          <el-input type="text" placeholder="请输入手机号" v-model="user.memberPhone" show-word-limit>
          </el-input>
        </el-form-item>
        <el-form-item>
          <div ref="dragDiv" class="drag">
            <div class="drag_bg"></div>
            <div class="drag_text">{{ confirmWords }}</div>
            <div ref="moveDiv" :class="{ 'handler_ok_bg': confirmSuccess }" class="handler handler_bg"
              style="position: absolute;top: 0px;left: 0px;" @mousedown="mousedownFn($event)"></div>
          </div>

        </el-form-item>
        <el-form-item style="color: aliceblue;" label="验证码" prop="yzm">
          <el-input type="text" placeholder="请输入验证码" v-model="user.yzm" show-word-limit>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('user')"> 登录 </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import cookie from 'js-cookie'

export default {
  //import引入的组件需要注入到对象中才能使用

  components: {
  },
  data () {
    return {
      user: {
        memberPhone: '18877429184',
        yzm: '',
      },
      userRules: {
        memberPhone: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          {
            pattern: /^1(3[0-9]|5[0-3,5-9]|7[1-3,5-8]|8[0-9])\d{8}$/,
            message: '请输入正确的手机号码',
          },
        ],
        yzm: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
        ]
      },
      beginClientX: 0, /* 距离屏幕左端距离 */
      mouseMoveStata: false, /* 触发拖动状态  判断 */
      maxwidth: '', /* 拖动最大宽度，依据滑块宽度算出来的 */
      confirmWords: '拖动滑块验证', /* 滑块文字 */
      confirmSuccess: false /* 验证成功判断 */
    }
  },
  mounted () {
    this.maxwidth = this.$refs.dragDiv.clientWidth - this.$refs.moveDiv.clientWidth;
    document.getElementsByTagName('html')[0].addEventListener('mousemove', this.mouseMoveFn);
    document.getElementsByTagName('html')[0].addEventListener('mouseup', this.moseUpFn)
  },
  methods: {
    //点击登录按钮
    submitForm (user) {
      this.$refs[user].validate((valid) => {
        if (valid) {
          this.$http.get('/menber/font/token', this.user).then((res) => {
            if (res.code == '0') {
              //保存token
              console.log(res)
              cookie.set('token', res.token, { domain: 'localhost' })
              cookie.set('userInfo', res.data, { domain: 'localhost' })
              this.$router.push('/home')
            }
            if (res.code != '0') {
              ElMessage.error({
                message: '手机号或者验证码错误',
                type: 'error',
              })
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    mousedownFn: function (e) {
      if (!this.confirmSuccess) {
        e.preventDefault && e.preventDefault(); // 阻止文字选中等 浏览器默认事件
        this.mouseMoveStata = true;
        this.beginClientX = e.clientX;
      }
    },
    // mousedoen 事件
    successFunction () {
      this.confirmSuccess = true
      this.confirmWords = '验证通过'
      this.$message({
        message: '验证通过，请查看验证码',
        type: 'success'
      });
      //发送验证码
      this.$http.get('/aliyun/send', this.user).then((res) => {
      })

      if (window.addEventListener) {
        document.getElementsByTagName('html')[0].removeEventListener('mousemove', this.mouseMoveFn);
        document.getElementsByTagName('html')[0].removeEventListener('mouseup', this.moseUpFn);
      } else {
        document.getElementsByTagName('html')[0].removeEventListener('mouseup', () => { });
      }
      document.getElementsByClassName('drag_text')[0].style.color = '#fff'
      document.getElementsByClassName('handler')[0].style.left = this.maxwidth + 'px';
      document.getElementsByClassName('drag_bg')[0].style.width = this.maxwidth + 'px';
    },
    // 验证成功函数
    mouseMoveFn (e) {
      if (this.mouseMoveStata) {
        let width = e.clientX - this.beginClientX;
        if (width > 0 && width <= this.maxwidth) {
          document.getElementsByClassName('handler')[0].style.left = width + 'px';
          document.getElementsByClassName('drag_bg')[0].style.width = width + 'px';
        } else if (width > this.maxwidth) {
          this.successFunction();
        }
      }
    },
    // mousemove事件
    moseUpFn (e) {
      this.mouseMoveStata = false;
      var width = e.clientX - this.beginClientX;
      if (width < this.maxwidth) {
        document.getElementsByClassName('handler')[0].style.left = 0 + 'px';
        document.getElementsByClassName('drag_bg')[0].style.width = 0 + 'px';
      }
    }
  },
  setup () {
    return {}
  },
}
</script>
<style scoped>
.card {
  margin: 0;
  padding: 0;
  width: 100vw;
  height: 100vh;
  background: radial-gradient(circle at 60% 90%,
      rgba(46, 103, 161, 1),
      transparent 60%),
    radial-gradient(circle at 20px 20px,
      rgba(46, 103, 161, 0.8),
      transparent 25%),
    #182336;
}

.login_Bj {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.drag {
  position: relative;
  background-color: #e8e8e8;
  width: 100%;
  height: 34px;
  line-height: 34px;
  text-align: center;
}

.handler {
  width: 40px;
  height: 32px;
  border: 1px solid #ccc;
  cursor: move;
}

.handler_bg {
  background: #fff url("") no-repeat center;
}

.handler_ok_bg {
  background: #fff url("") no-repeat center;
}

.drag_bg {
  background-color: #7ac23c;
  height: 34px;
  width: 0px;
}

.demo-ruleForm {
  color: hsl(215, 25%, 70%);
}

.drag_text {
  position: absolute;
  top: 0px;
  width: 100%;
  text-align: center;
  -moz-user-select: none;
  -webkit-user-select: none;
  user-select: none;
  -o-user-select: none;
  -ms-user-select: none;
}
</style>